<!--
 Copyright 2024 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<mat-dialog-content>
  <h2 mat-dialog-title>New inspection</h2>
  <mat-stepper
    [linear]="true"
    #stepper
    (selectedIndexChange)="selectedStepChange($event)"
  >
    <!--Step1 : Select the inspection types-->
    <mat-step [completed]="(currentInspectionType | async) !== null">
      <div class="step1-scroll-wrapper">
        <form>
          <ng-template matStepLabel>
            <span *ngIf="currentInspectionType | async; else nonSelected"
              >Type: {{ (currentInspectionType | async)!.name }}</span
            >
            <ng-template #nonSelected>Select the inspection target</ng-template>
          </ng-template>
          <div class="inspection-type-spacer"></div>
          <ng-container
            *ngIf="inspectionTypes | async; else loading; let types"
          >
            <mat-card
              *ngFor="let inspectionType of types.types"
              class="inspection-type"
              (click)="setInspectionType(inspectionType)"
            >
              <mat-card-header>
                <mat-card-title>{{ inspectionType.name }}</mat-card-title>
              </mat-card-header>
              <mat-card-content>
                <div class="inspection-type-icon">
                  <img [src]="inspectionType.icon" />
                </div>
                <div
                  class="inspection-type-description"
                  [innerHTML]="inspectionType.description | breakline"
                ></div>
              </mat-card-content>
            </mat-card>
          </ng-container>
          <ng-template #loading>
            <p>Loading list of inspection types...</p>
            <mat-progress-bar mode="indeterminate"></mat-progress-bar>
          </ng-template>
        </form>
      </div>
    </mat-step>
    <!--Step2 : Select enabled features-->
    <mat-step>
      <ng-template matStepLabel>Select what you want to query</ng-template>
      <div class="features-page">
        <div class="features-wrapper">
          <mat-card
            *ngFor="let feature of currentTaskFeatures | async"
            (click)="toggleFeature(feature.id)"
            [ngClass]="{ selected: feature.enabled }"
          >
            <span *ngIf="feature.enabled" class="selected-icon">
              <mat-icon>check_circle</mat-icon>
            </span>
            <mat-card-header>
              <mat-card-title>{{ feature.label }}</mat-card-title>
            </mat-card-header>
            <mat-card-content [innerHTML]="feature.description | breakline">
            </mat-card-content>
          </mat-card>
        </div>
        <div class="features-footer">
          <p
            *ngIf="(featuresEnabled | async) === false"
            class="errmsg-no-selected-feature"
          >
            Select at least a feature
          </p>
          <button
            [disabled]="(featuresEnabled | async) === false"
            mat-flat-button
            color="primary"
            matStepperNext
          >
            Next
          </button>
        </div>
      </div>
    </mat-step>
    <!--Step3 : Input parameters-->
    <mat-step>
      <ng-template matStepLabel>Input parameters</ng-template>
      @if (parameterViewModel | async; as parameterViewModel) {
        <div class="parameter-view">
          <div class="parameters-form-view allow-vertical-scroll">
            <khi-new-inspection-group-parameter
              [parameter]="parameterViewModel.rootGroupForm"
            ></khi-new-inspection-group-parameter>
          </div>
          <div class="parameters-metadata-view allow-vertical-scroll">
            <mat-card class="query-section-card">
              <mat-card-header>
                <mat-card-title>Query</mat-card-title>
              </mat-card-header>
              <mat-card-content>
                @for (query of parameterViewModel.queries; track query.name) {
                  <mat-card class="query-card">
                    <mat-card-header>
                      <mat-card-title>{{ query.name }}</mat-card-title>
                    </mat-card-header>
                    <mat-card-content>
                      <pre class="query-section-query-pre">{{
                        query.query
                      }}</pre>
                    </mat-card-content>
                  </mat-card>
                }
              </mat-card-content>
            </mat-card>
            <mat-card>
              <mat-card-header>
                <mat-card-title>Inspection plan</mat-card-title>
              </mat-card-header>
              <mat-card-content>
                <p>
                  Copy the following task execution plan and visualize it with
                  graphviz.
                </p>
                <pre class="task-graph-pre">{{
                  parameterViewModel.plan.taskGraph
                }}</pre>
              </mat-card-content>
            </mat-card>
          </div>
          <div class="parameters-footer">
            <p
              *ngIf="parameterViewModel.errorFieldCount !== 0"
              class="errmsg-parameters-error"
            >
              Fix the validation errors on the input parameters:
              {{ parameterViewModel.errorFieldCount }} /
              {{ parameterViewModel.fieldCount }}
            </p>
            <button
              [disabled]="hadRun() || parameterViewModel.errorFieldCount !== 0"
              mat-raised-button
              color="primary"
              (click)="onRunButtonClick()"
            >
              Run
            </button>
          </div>
        </div>
      } @else {
        <div class="progress-parameters-forms">
          <mat-progress-bar mode="indeterminate"></mat-progress-bar>
          <p class="message">
            Loading a parameter list for the given setting. Wait a few seconds.
          </p>
        </div>
      }
    </mat-step>
  </mat-stepper>
</mat-dialog-content>
